import { request } from "./request";

async function listDevice() {
  const response = await request("/device", "GET", null, true);
  if (!Array.isArray(response)) {
    throw Error("Invalid response");
  }
  return response;
}

async function updateDevice(deviceId, newInfo) {
  if (typeof newInfo != "object") {
    throw Error("Invalid argument");
  }
  await request(`/device/${deviceId}`, "PATCH", newInfo, true);
}

async function createDevice(deviceinfo) {
  const response = await request("/device/create", "POST", deviceinfo, true);
  if (typeof response != "object") {
    throw Error("Invalid response");
  }
  return response;
}

async function deleteDevice(id) {
  if (typeof id != "number" || (id != (id | 0))) {
    throw Error("Invalid argument");
  }
  await request(`/device/${id}`, "DELETE", null, true);
}

export { listDevice, createDevice, updateDevice, deleteDevice };